#define _CRT_SECURE_NO_WARNINGS 1
struct ListNode {
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};
 
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head)
    {
        if (head == nullptr) return head;
        ListNode* prev = head;
        if (head->next == nullptr) return head;
        ListNode* cur = head->next;
        while (cur != nullptr)
        {
            ListNode* tmp = cur->next;
            if (cur->val == prev->val)
            {
                prev->next = tmp;
            }
            else
            {
                prev = prev->next;
            }
            cur = tmp;
        }
        return head;
    }
};